package org.example.myleet.p1051;

public class Solution {
    public int heightChecker(int[] heights) {
        int n = heights.length;
        int[] heightCount = new int[101];
        for (int i = 0; i < n; ++i) {
            ++heightCount[heights[i]];
        }
        int incoordinateCount = 0;
        int j = 0;
        for (int i = 0; i < n; ++i) {
            while (heightCount[j] < 1) {
                ++j;
            }
            if (heights[i] != j) {
                ++incoordinateCount;
            }
            --heightCount[j];
        }
        return incoordinateCount;
    }
}
